<template>
  <div >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="供应商名称" prop="corporateName">
        <el-input
          v-model="queryParams.corporateName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一信用代码" prop="unifiedCreditCode">
        <el-input
          v-model="queryParams.unifiedCreditCode"
          placeholder="请输入统一信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column label="供应商名称" align="center" prop="corporateName" />
      <el-table-column label="统一信用代码" align="center" prop="unifiedCreditCode" />
      <el-table-column label="法人" align="center" prop="legalPerson" />
      <el-table-column label="法人电话" align="center" prop="legalPhone" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="上传的营业执照复印件" align="center" prop="businessLicense">
        <template slot-scope="scope">
          <el-image 
            v-if="scope.row.businessLicense"
    style="width: 100px; height: 100px"
    :src="baseUrl + scope.row.businessLicense" 
    :preview-src-list="srcList"
    @click="vbs(baseUrl + scope.row.businessLicense)">
  </el-image>
          <span v-else>暂无数据</span>
        </template>
      </el-table-column>
      <el-table-column label="公司类别" align="center" prop="type" :show-overflow-tooltip="true" >
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="state" >
        <template slot-scope="scope">
        <el-tag type="danger" v-if="scope.row.state === '0'" style="margin-left: 10px">待审核</el-tag>
        <el-tag type="success" v-else-if="scope.row.state === '1'" style="margin-left: 10px">审核通过</el-tag>
        <el-tag type="warning" v-else-if="scope.row.state === '2'" style="margin-left: 10px">审核未通过</el-tag>
      </template>
      </el-table-column>
      <el-table-column label="经办人" align="center" prop="handledBy" />
      <el-table-column label="经办人联系电话" align="center" prop="handledByPhone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="openTypeDialog(scope.row)"
          >修改公司类别</el-button>
          <el-button
            size="mini"
            type="text"
            @click="del(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <DialogView
      v-if="typeDialog.visible"
      :dialogTitle="typeDialog.dialogTitle"
      :visible="typeDialog.visible"
      @handleClose="handleClose"
      @submitData="submitData"

    >
      <el-form :model="rowData">
        <el-form-item label="目前公司类别">
          <span>{{rowData.type}}</span>
        </el-form-item>
        <el-form-item label="公司类别">
        <el-cascader v-model="rowData.list" :props="cascaderProps" collapse-tags placeholder="请选择公司类别"> </el-cascader>
        </el-form-item>
      </el-form>
    </DialogView>
  </div>
</template>

<script>
import { listSupplier,delSupplier,typeAllList } from "@/api/supplier/api";
import { updateSupply } from "@/api/system/user";

import { listMaterial, listType } from "@/api/purchase/api";
export default {
  computed:{
    cascaderProps(){
        return{
          lazy:true,
          multiple: true,
          lazyLoad:this.lazyLoad,
          emitPath: false
        }
      },
  },
  name: "reviewed",
  data() {
    return {
      typeDialog:{
        dialogTitle:'修改公司类别',
        visible:false
      },
      srcList:[],
      baseUrl : process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商信息表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        corporateName: null,
        unifiedCreditCode: null,
        legalPerson: null,
        legalPhone: null,
        address: null,
        businessLicense: null,
        type: null,
        state: '1',
        handledBy: null,
        handledByPhone: null,
        password: null,
        email: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      rowData:{
        list:[]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    submitData() {
      const data = { };
      data.supplierId = this.rowData.supplierId;
      data.typeIds = this.rowData.list.join(",");
      updateSupply(data).then((res) => {
        this.$modal.msgSuccess("修改成功");
        this.typeDialog.visible = false;
        this.getList();
      });
    },
    handleClose(){
      this.typeDialog.visible = false
    },
    //懒加载
    async lazyLoad(node, resolve){
      let level = node.level
      let result
      switch (level) {
        case 0:
          let initRes = await listMaterial()
          result = initRes.rows
          result.forEach((item) =>{
            item.value = item.materialTypeId
            item.label = item.materialTypeName
          })
          break;
        case 1:
          let firstParams = {
            typeId:node.data.value
          }
        let firstRes = await listType(firstParams)
          result = firstRes.data
          result.forEach((item) =>{
            item.value = item.materialId
            item.label = item.typeName
            item.leaf = level >= 1
          })
          break;
      
        default:
          break;
      }
      resolve(result)
    },
    openTypeDialog(row){
      this.rowData = row
      console.log('this.rowData',this.rowData)
      this.typeDialog.visible = true
    },
    vbs(val){
      this.srcList = []
      this.srcList.push(val)
    },
    /** 查询供应商信息列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then(response => {
        // this.supplierList = response.rows;
        const data = response.rows;
        this.total = response.total;
        this.loading = false;
        data.forEach((e) =>{
            e.typeIds = e.typeIds.split(',').map(Number)
        })
        this.supplierList = data
        console.log('data',data)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        corporateName: null,
        unifiedCreditCode: null,
        type: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.supplierId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 审核按钮操作 */
    del(row) {
      const supplierIds = row.supplierId || this.ids;
      this.$modal.confirm('是否确认删除供应商名称为"' + row.corporateName + '"的数据项？').then(function() {
        return delSupplier(supplierIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/supplier/export', {
        ...this.queryParams
      }, `supplier_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
